# -*- coding:utf-8 -*-
import time
from selenium.webdriver.common.by import By
from PageClass.common.boeCommon import BoeCommon
from PageClass import BasIndexPage
from Util import logger


class Class_WithholdingAmortizationBoe(BasIndexPage, BoeCommon):
    def __init__(self, driver):
        super().__init__(driver)

    # 打开流水管理页面
    def goToBoePage(self, url):
        self.driver.get(url)
        logger.info("打开流水管理页面")


    # 主表区-业务类型
    def input_operationType(self, operationTypeName):
        _operationType = (By.ID, "boeHeader.0.operationTypeId")
        self.lovSelectTable(operationTypeName, *_operationType)
        logger.info("输入主表区-业务类型字段")

    # 主表区-备注
    def input_boeAbstract(self, boeAbstract):
        _boeAbstract = (By.ID, "boeHeader.0.boeAbstract")
        self.send_text(boeAbstract, *_boeAbstract)
        logger.info("输入主表区-备注字段")

    # 主表附加区-客商
    def input_vendor(self, vendorName):
        _vendorId = (By.ID, 'boeHeaderChild.0.vendorId')
        try:
            self.lovSelectTable(vendorName, *_vendorId)
        except:
            self.click(*_vendorId)
            time.sleep(3)
            # 选第一个
            _line_1 = (By.XPATH, '//*[@id="boeHeaderChild.0.vendorId.table"]/div[3]/div/div[2]/table[1]/tbody/tr[1]/td[1]/div/div')
            self.click(*_line_1)
        logger.info("输入主表附加区-客商字段")

    # 主表附加区-客商2
    def input_vendor2(self, vendorName):
        _vendorId = (By.ID, 'boeHeaderChild.0.vendorName')
        self.click(*_vendorId)
        self.selectValueFromSmallTable(**vendorName)
        logger.info("输入主表附加区-客商字段")

    # 主表附加区-预提规则
    def input_rule(self, ruleName):
        _ruleId = (By.ID, 'boeHeaderChild.0.rule')
        self.click(*_ruleId)
        # self.lovSelectTable(ruleName, *_ruleId, tip='span')
        # 选第一个
        self.click(*(By.XPATH, f'//span[text()="{ruleName}"]'))
        logger.info("输入主表附加区-预提规则字段")

    # 费用区-预提类型,num:行数
    def input_operationSubType(self, operationSubTypeName, num: int = 1):
        num = str(int(num)-1)
        _operationSubTypeId = (By.ID, f'cost.{num}.operationSubTypeId')
        self.click(*_operationSubTypeId)
        self.lovSelectTable(operationSubTypeName, *_operationSubTypeId)
        logger.info("输入费用区-预提类型字段")

    # 费用区-责任部门,num:行数
    def input_expenseDept(self, expenseDeptCode, num: int = 1):
        num = str(int(num)-1)
        _expenseDeptId = (By.ID, f'cost.{num}.expenseDeptId')
        self.click(*_expenseDeptId)
        self.selectValueFromSmallTable(**{"编码": expenseDeptCode})
        logger.info("输入费用区-责任部门字段")

    # 费用区-金额,num:行数
    def input_expenseAmount(self, expenseAmount, num: int = 1):
        num = str(int(num)-1)
        _expenseAmountId = (By.XPATH, f'//div[@id="cost.{num}.expenseAmount"]')
        self.send_text(expenseAmount, *_expenseAmountId)
        logger.info("输入费用区-金额字段")

    # 费用区-开始日期,num:行数
    def input_beginDate(self, num: int = 1):
        num = str(int(num)-1)
        _beginDateId = (By.XPATH, f'//input[@id="cost.{num}.beginDateStr"]')
        self.click(*_beginDateId)
        time.sleep(1)
        self.select_date(time.strftime('%Y-%m-%d').split('-')[0], time.strftime('%Y-%m-%d').split('-')[1],
                         time.strftime('%Y-%m-%d').split('-')[2])  # 取当天
        logger.info("输入费用区-开始日期字段")

    # 费用区-右移控件
    def find_scrollToRight(self):
        try:
            _deadline = (By.XPATH, '//div[@class="area-data-scroll is-scrolling is-scrolling-middle"]')
            scroll = self.find_element(*_deadline)
        except:
            _deadline = (By.XPATH, '//div[@class="area-data-scroll is-scrolling is-scrolling-left"]')
            scroll = self.find_element(*_deadline)
        self.scrollToRight(scroll)

    # 费用区-期限（月）
    def input_deadline(self, deadline):
        _deadline = (By.XPATH, '//input[@class="el-input__inner" and @aria-disabled="false"]')
        self.send_text(deadline, *_deadline)
        logger.info("输入费用区-期限（月）字段")

    # 费用区-贷方科目
    def input_accountId(self, accountCode, num: int = 1):
        num = str(int(num)-1)
        _accountId = (By.ID, f'cost.{num}.accountId')
        self.lovSelectTable(accountCode, *_accountId)
        logger.info("输入费用区-贷方科目字段")

    # 费用区-费用行-自动计算 # 确认计算结果
    def click_autocalcu(self):
        _autocalcu = (By.XPATH, '//div[@class="el-form-item operate area-control el-form-item--small"]/div/button[@class="el-button rever-btn el-button--text el-button--mini"]/span[text()=" 自动计算 "]')
        # self.click(*_autocalcu)
        element1 = self.find_element(*_autocalcu)
        self.executeScript("arguments[0].click();", element1)

        time.sleep(2)
        _button_ok = (By.XPATH, '//button[@class="el-button el-button--primary el-button--mini"]/span')
        self.click(*_button_ok)

    # 获得提单结果提示语
    BoeSuccessMsg = (By.XPATH, '//*[@id="app"]/div/div/div[4]/div/div/div[2]/div/p')
    def getBoeSuccessMessage(self):
        # 提单成功结果页面提示语
        return self.get_elementText(*self.BoeSuccessMsg)

